/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment2;

import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * ExternalFileWriter Class:
 * This class is used to write all the current product and inventory information to an external file, such that it can be used at a later date if need be.
 * @author Vale
 *
 */
public class ExternalFileWriter {

    private static final String FILE_NAME = "E:\\SCMSInfo.xml";

    /**
     * ExternalFileWriter Constructor:
     * This constructor is used to write the product list and inventory list information to an external file
     *
     */
    public ExternalFileWriter(ProductList prodList, InventoryList invList) throws IOException {
        XMLEncoder writeFile = null;
        int prodItems = prodList.size();
        int invItems = invList.size();
        try {
            FileOutputStream fos = new FileOutputStream(FILE_NAME);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            writeFile = new XMLEncoder(bos);
        } catch (FileNotFoundException ex) {
            File externalFile = new File(FILE_NAME);
            externalFile.createNewFile();
        }
        writeFile.writeObject(prodItems);
        writeFile.writeObject(invItems);
        for (int i = 0; i < prodItems; i++) {
            writeFile.writeObject(prodList.get(i).getProdName());
            writeFile.writeObject(prodList.get(i).getProdCost());
            writeFile.writeObject(prodList.get(i).getProdSched());
            writeFile.writeObject(prodList.get(i).getInvSize());
            InventoryList currProdInv = prodList.get(i).getInventList();
            for (int j = 0; j < currProdInv.size(); j++) {
                writeFile.writeObject(currProdInv.get(j).getInvName());
                writeFile.writeObject(currProdInv.get(j).getInvCost());
                writeFile.writeObject(currProdInv.get(j).getInvQuality());
                writeFile.writeObject(currProdInv.get(j).getInvUnits());
                writeFile.writeObject(currProdInv.get(j).getInvStock());
            }
        }

        for (int k = 0; k < invItems; k++) {
            Inventory currInvItem = invList.get(k);
            writeFile.writeObject(currInvItem.getInvName());
            writeFile.writeObject(currInvItem.getInvCost());
            writeFile.writeObject(currInvItem.getInvQuality());
            writeFile.writeObject(currInvItem.getInvUnits());
            writeFile.writeObject(currInvItem.getInvStock());
        }
        writeFile.close();

    }
}
